package com.zgc.view.controller;

import com.fx.spring.annotation.ControllerFX;
import com.fx.spring.controller.AbstractController;
import com.fx.spring.util.FxmlLoaderUtil;
import com.fx.spring.util.SpringContextUtil;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXTabPane;
import com.zgc.cache.ConnectionInfoCache;
import com.zgc.cache.InformationTabCache;
import com.zgc.constant.ZkConstant;
import com.zgc.util.JsonUtil;
import com.zgc.view.common.PopupCtrl;
import com.zgc.zkinfo.ZkConnectionInfo;
import com.zgc.zkinfo.ZkNodeInfo;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.ObservableMap;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.geometry.Pos;
import javafx.scene.control.Tab;
import javafx.scene.layout.VBox;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;

import javax.xml.transform.Source;
import java.io.File;
import java.net.URL;
import java.util.*;

@ControllerFX(fxmlURL = "/view/fxml/InformationTable.fxml")
public class InformationTableCtrl extends AbstractController<JFXTabPane> {


    @FXML
    private JFXTabPane infoTablePane;
    @FXML
    private Tab homeTab;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        logger.debug("[{}]初始化完成", location);
    }

    @Autowired
    private PopupCtrl popupCtrl;

    /**
     * 测试按钮后面删除
     */
    @FXML
    private void testButton(ActionEvent actionEvent) {
        popupCtrl.enqueueRoot("提示", "test");
    }


    /**
     * 获取 Tab 列表
     *
     * @return ObservableList<Tab>
     */
    public ObservableList<Tab> getTabs() {
        return infoTablePane.getTabs();
    }

    /**
     * 添加 tab
     *
     * @param zkConnectionInfo ZkConnectionInfo
     */
    public void addTab(ZkConnectionInfo zkConnectionInfo) {
        logger.debug("添加Tab 连接信息[{}]", zkConnectionInfo);
        InformationTabCtrl informationTabCtrl = InformationTabCache.getTab(zkConnectionInfo.getId());
        if (informationTabCtrl == null) {
            informationTabCtrl = SpringContextUtil.getBean(InformationTabCtrl.class);
            Tab tab = informationTabCtrl.load(informationTabCtrl);
            getTabs().add(tab);

        }
        Tab currentTab = informationTabCtrl.load();
        if (!getTabs().contains(currentTab)) {
            getTabs().add(currentTab);
        }
        informationTabCtrl.setTextAndLoad(zkConnectionInfo);
        infoTablePane.getSelectionModel().select(currentTab);
        InformationTabCache.putTab(zkConnectionInfo.getId(), informationTabCtrl);

        //删除监听
        zkConnectionInfo.connectedProperty().addListener((obv, ov, nv) -> {
            if (!nv) {
                removeTab(zkConnectionInfo);
            }
        });
    }


    public void removeTab(ZkConnectionInfo zkConnectionInfo) {
        InformationTabCtrl tabCtrl = InformationTabCache.getTab(zkConnectionInfo.getId());
        if (tabCtrl != null) {
            getTabs().remove(tabCtrl.load());
        }
    }
}
